package com.example.shop_2211.fgViewModel

import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.viewModelScope
import com.example.base.entity.CarEntityResult
import com.example.base.entity.PreOrderEntityParam
import com.example.base.entity.PreOrderEntityResult
import com.example.base.entity.UpdateEntityResult
import com.example.base.mvvm.BaseViewModel
import kotlinx.coroutines.launch

class CartViewModel:BaseViewModel() {
    val getSuccess = MutableLiveData<CarEntityResult>()
    val getFail = MutableLiveData<String>()
    fun getCar(isValid:String,limit:String,page:String){
        viewModelScope.launch {
            try {
                getSuccess.value = apiService.getCar(isValid, limit, page)
            }catch (e:Exception){
                getFail.value = e.message
            }
        }
    }
    val updateSuccess = MutableLiveData<UpdateEntityResult>()
    val updateFail = MutableLiveData<String>()
    fun updateCar(id:Int,number:Int){
        viewModelScope.launch {
            try {
                updateSuccess.value = apiService.updateCar(id, number)
            }catch (e:Exception){
                updateFail.value = e.message
            }
        }
    }
    val deleteSuccess = MutableLiveData<UpdateEntityResult>()
    val deleteFail = MutableLiveData<String>()
    fun deleteCar(ids:Array<Int>){
        viewModelScope.launch {
            try {
                deleteSuccess.value = apiService.deleteCar(ids)
            }catch (e:Exception){
                deleteFail.value = e.message
            }
        }
    }
    //预下单
    val preOrderSuccess = MutableLiveData<PreOrderEntityResult>()
    val preOrderFail = MutableLiveData<String>()
    fun preOrder(body:PreOrderEntityParam){
        viewModelScope.launch {
            try {
                preOrderSuccess.value = apiService.preOrder(body)
            }catch (e:Exception){
                preOrderFail.value = e.message
            }
        }
    }
}